﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Application.AppServices.UserManagement;
using Framework.Infrastructure;
using Domain.Module.Entities;
using Infrastructure.DataObjects;
using Infrastructure.QueryServices;
using Framework.Infrastructure.IoC;
using Microsoft.Practices.Unity;
using Application.AppServices.CommentManagement;
using Microsoft.Practices.Unity.Configuration;
using System.Configuration;
using Application.AppServices.MBlogManagement;
using Infrastructure.DataObject;

namespace IHaveBaby
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        IUserManagementService UserService { get; set; }
        ICommentManagementService CommentService { get; set; }
        IMBlogManagementService MBlogService { get; set; }
        public WebForm1() { }
        public WebForm1(IUserManagementService userService,
            ICommentManagementService commentService,
            IMBlogManagementService mblogService)
        {
            UserService = userService;
            CommentService = commentService;
            MBlogService = mblogService;
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            var userQueryService = IoCFactory.Instance.CurrentContainer.Resolve<IUserQueryService>();//ObjectFactory.NewIocInstance<IUserQueryService>();
            //var users = userQueryService.FindUsersByName("n").ToList();
            //var someuser = userQueryService.FindUserByName("Ivan");
            // var userService = IoCFactory.Instance.CurrentContainer.Resolve<IUserManagementService>();
            #region Add user
            var userID = new Guid("37fc1a8f-92cb-4598-b631-913977755c51");
            var user = userQueryService.FindUser(userID);
            if (user == null)
            {
                var userObject = new UserObject
                {
                   ID = userID,
                   Name = "Ivan"
               };
               UserService.AddUser(userObject);
               user = userQueryService.FindUser(userID);
            }
            
            
            #endregion

            #region publish mblog
            var mblog = new MBlogObject 
            {
                ID = Guid.NewGuid(),
                Body = "test mblog publish",
                CreateTime = DateTime.Now
            };
            MBlogService.PublishMBlog(userID, mblog);
            user.CollectionLoad(u => u.MBlogs);

            var mblogsquery = user.GetQueryable(u => u.MBlogs);
            #endregion

            #region publish mblog comment
            CommentService.PublishMBlogComment(userID,
                                               mblog.ID,
                                               new CommentObject
                                               {
                                                   ID = Guid.NewGuid(),
                                                   Body = "test",
                                                   CreateTime = DateTime.Now
                                               });
            #endregion
            //user = new UserObject { ID = userID, Name = "Ivan" };
            //if (user != null)
            //{
            //    UserService.UpdateUser(user);
            //}
        }
    }
}